import DataUtil from "../../utils/DataUtil.js";
//加载第三方依赖，实现JS的异步转同步方向
import regeneratorRuntime from "../../libs/regenerator-runtime/runtime.js";
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    recommendList:[],
    currentVideoId: null
  },

  attached:function(){
    this.getRecommendList();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getRecommendList(){
      if(wx.getStorageSync('recommendList')){
        this.setData({
          recommendList:wx.getStorageSync('recommendList')
        });
      }
      else{
        wx.showLoading({
          title: '正在加载数据'
        });
        var previousRecommendIds = wx.getStorageSync('previousRecommendIds')||[0,0];
        var result = await DataUtil.getRecommendList(previousRecommendIds);
        if(result.list.length>0){
          this.setData({
            recommendList:result.list
          });
          wx.setStorageSync('recommendList', result.list);
          var startId = result.list[0].id;
          var endId = result.list[result.list.length-1].id;
          wx.setStorageSync('previousRecommendIds', [startId,endId]);
        }
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    },
    //预览图片的方法
    previewCurrentImage(event){
      //思考：这个预览的图片怎么拿过来
      //data-imgsrc    event.currentTarget.dataset.imgsrc
      var imgsrc = event.currentTarget.dataset.imgsrc;
      wx.previewImage({
        urls:[imgsrc]
      });
    },
    //获取分享链接
    getShareUrl(event){
      //data-shareurl       
      var shareurl = event.currentTarget.dataset.shareurl;
      //将这个url地址设置到系统的剪切板
      wx.setClipboardData({
        data:`看了这个 ，我和我的小伙伴都惊呆了！！！${shareurl}`,
        success:()=>{
          wx.showToast({
            title:"快去发给你的小伙伴看看吧",
            icon:"none",
            duration:5000
          });
        }
      });

    },
    //去详细页面
    toDetail(event){
      var index = event.currentTarget.dataset.itemindex;
      var currentItem = this.data.recommendList[index];
      //将当前项放入缓存 
      wx.setStorageSync('currentItem', currentItem);
      wx.navigateTo({
        url: "/pages/detail/detail"
      });
    },
    videoListScroll(event) {
      //在这里，我们就可以监听用户的滚动行为
      //只要是播放的视频移到界面外边去了以后，我就可以让它暂停
      if (this.data.currentVideoId != null) {
        //说明已经有视频在播放了
        //你要去判断一下，这个正在播放的视频是否移到的屏幕外边去了
        // #代表ID
        this.createSelectorQuery().select("#" + this.data.currentVideoId).boundingClientRect(rect => {
          // console.log(rect.top);
          //我们要判断当前视频距离上边的距离 ，如果移出去了，就代表我们要停止这个视频 
          //上滑的时候 判断是否超过屏幕的高度
          var screenHiehgt = wx.getSystemInfoSync().windowHeight;
          if (rect.top < rect.height * (-1) || rect.top > screenHiehgt) {
            this.videoContext = wx.createVideoContext(this.data.currentVideoId, this);
            this.videoContext.stop();
            this.setData({
              currentVideoId: null
            });
          }
        }).exec();
      }
    },
    setCurrentVideoId(event) {
      //如果之前不为空，则将之前的视频也暂停掉
      if(this.data.currentVideoId!=null){
        wx.createVideoContext(this.data.currentVideoId, this).stop();
      }
      var videoid = event.currentTarget.dataset.videoid;
      this.setData({
        currentVideoId: videoid
      }, () => {
        setTimeout(() => {
          this.videoContext = wx.createVideoContext(this.data.currentVideoId, this);
          this.videoContext.play();
        }, 500);
      });

    },
    //视频播放的时候，拿到当前视频的ID
    videoPlay(event) {
      var videoid = event.currentTarget.dataset.videoid;
      this.setData({
        currentVideoId: videoid
      })
    }
  }
})
